package com.fwc.churchtracker.messaging;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import com.fwc.churchtracker.domo.Member;
import com.fwc.fw.service.DataStoreService;

import javax.servlet.ServletContext;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.appengine.api.users.UserServiceFactory;

public class RegistrationService extends MessagingService {

    public RegistrationService(ServletContext servletContext, String toAddress) {
		super(servletContext, toAddress);
	}

	public String receive(String fromAddress, String body) {
		
		    ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(context);
		    DataStoreService dss = (DataStoreService) ac.getBean("dataStoreService");
			Member fromMember = (Member) dss.findByEmail(Member.class,fromAddress);

		    if(null!=fromMember) {
		    	return "You're already registered, " + fromMember.getFirstName()+"!";
		    } else {

				if(null == context.getAttribute("_chatReg")) {
					context.setAttribute("_chatReg", new HashMap<String,Member>());
				}
				HashMap<String,HashMap<String,Object>> hash = (HashMap<String, HashMap<String,Object>>) context.getAttribute("_chatReg");
				if(hash.get(fromAddress) == null) {
					hash.put(fromAddress, new HashMap<String,Object>());
				}
				HashMap<String,Object> sess = hash.get(fromAddress);
				if(sess.get("member") == null) {
					sess.put("member", new Member());
				}
				Member m = (Member) sess.get("member");
				m.setEmail(fromAddress);
				m.setActive(true);

				String q = (String) sess.get("q");
				Object a = sess.get("a");
				
				if(null!=q && !q.isEmpty() && q.startsWith("confirm:")) {
					if(body.toLowerCase().equals("yes")) {
						try {
							BeanUtils.setProperty(m, q.replaceFirst("confirm:", ""), a);
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						}
						sess.put("q", null);
						sess.put("a", null);
					} else if(body.toLowerCase().equals("no")) {
						sess.put("q", null);
						sess.put("a", null);
					} else {
						return "please reply with 'yes' or 'no'.";
					}
				} else if(null!=q && !q.isEmpty() && body!=null && !body.isEmpty()) {
					sess.put("q", "confirm:"+q);
					sess.put("a", body);
					return "You answered: '"+body+"'.  Is this correct? (reply with yes or no)";
				}
					
				if(null==m.getFirstName() || m.getFirstName().isEmpty()) {
					sess.put("q", "firstName");
					return "To register, please answer a few questions.  What's your first name?";
				} else if(null==m.getLastName() || m.getLastName().isEmpty()) {
					sess.put("q", "lastName");
					return "What's your last name?";
				} else if(null==m.getPhone() || m.getPhone().isEmpty()) {
					sess.put("q", "phone");
					return "What's your phone number?";
				} else {
					dss.save(m);
					return "Thanks for registering, " + m.getFirstName()+"!";
				}
			}
	}
}
